<template>
  <div>
    <van-nav-bar title="注册" left-arrow @click-left="$router.back()" />

    
    <van-form @submit="onSubmit" ref="regForm">
      <van-field
        left-icon="gem"
        v-model="username"
        name="username"
        label="手机号"
        placeholder="手机号"
        :rules="[
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' },
        ]"
      />

      <van-field
        left-icon="gem"
        v-model="vercode"
        name="vercode"
        clearable
        label="验证码"
        placeholder="验证码"
        :rules="[
          { required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码错误' },
        ]"
      >
        <template #button>
          <van-button
            size="mini"
            @click="send"
            native-type="button"
            :disabled="btnDisabled"
            >{{ code_text }}</van-button
          >
        </template>
      </van-field>

      <van-field
        left-icon="gem"
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请输入密码' },
          {
            validator: (e) => /^\w{6,12}$/.test(e),
            message: '密码格式为6-12位字母或数字',
          },
        ]"
      />
      <van-button
        color="#4fc08d"
        block
        native-type="submit"
        :loading="submitLoading"
        >确定</van-button
      >
    </van-form>
  </div>
</template>

<script>
import { reg, sendSms, delete_user } from "@/api/zhuce";
export default {
  name: "zhuce",
  components: {},
  data() {
    return {
      username: "",
      password: "",
      vercode: "",
      btnDisabled: false,
      code_text: "发送验证码",
      submitLoading: false,
    };
  },
  created() {
    // delete_user({
    //   username:'17344761262',
    //   password:'123456'
    // })
  },
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      this.submitLoading = true;
      reg(values)
        .then((res) => {
          console.log(res);
        })
        .finally((e) => {
          this.submitLoading = false;
        });
    },

    //方法
    async send() {
      try {
        this.$refs.regForm.validate("username"); //
        //

        this.btnDisabled = true; //不可点击

        this.code_text = "发送中...";
        let timer = null;
        let count = 10;

        let res = await sendSms({
          mobile: this.username,
          type: "register",
        })
          .then((res) => {
            if (res.code == 0) {
              this.$toast(`验证码已经发送到${this.username}请注意查收`);

              timer = setInterval((e) => {
                count--;
                this.code_text = `${count}s后再次发送`;
                if (count <= 0) {
                  clearInterval(timer);
                  count = 10;
                  this.code_text = "再次获取";
                  this.btnDisabled = false;
                }
              }, 1000);
            } else {
              this.$toast(res.msg);
              this.btnDisabled = false; //不可点击

              this.code_text = "再次发送";
            }
          })
          .catch((err) => {
            //提示失败  再次发送  恢复到可以点击的状态

            this.$toast(res.msg || "网络错误");

            this.btnDisabled = false; //不可点击

            this.code_text = "再次发送";
          });

        //  console.log(res)
      } catch (error) {}

      // .then(res=>{
      //     console.log(res)
      // }).catch(err=>{
      //     console.log(err)
      // })
      // .then(res=>{

      // })
    },
  },
};
</script>

<style lang="scss">
.van-tag--default {
  margin-left: 270px;
  bottom: 35px;
  color: #000;
  background-color: #ebecec;
}
.van-tag {
  line-height: 6vw;
}
</style>
